package com.myebills.ebpsfrontend.cust.maxis;

import java.io.*;
import java.util.ArrayList;

import org.w3c.dom.*;
import javax.xml.parsers.*;

public class DOMParser {

	public static void main(String[] args) throws Exception {
		//File f = new File("D:/response.xml");
		File f = new File("D:/Workspace/Maxis/EBPS/sso/data/response.xml");
		DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
		DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
		Document doc = docBuilder.parse(f);
		//Node root = doc.getDocumentElement();

		NodeList nodeList = doc.getElementsByTagName("GetLOBInfo_XMLResult");
		Node root = (Element)nodeList.item(0);

		LobOutputDao objOutput = new LobOutputDao();
		objOutput = retrieveValue00(root, 1);

		// Print output
		System.out.println("....Start print output....");

		System.out.println("Status = " + objOutput.getStatus());
		System.out.println("Error_Code = " + objOutput.getErrCode());
		System.out.println("Error_Msg = " + objOutput.getErrMsg());

		ArrayList listAcc = objOutput.getAccount();
		for (int i = 0; i < listAcc.size(); i++) {
			AccountInfoDao objAccInfo = new AccountInfoDao();
			objAccInfo = (AccountInfoDao) listAcc.get(i);

			System.out.println("acctExtID = " + objAccInfo.getAcctExtID());

			ArrayList listAccDetails = objAccInfo.getAcctDetails();
			for (int j = 0; j < listAccDetails.size(); j++) {
				AccountDetailsInfoDao objAccDetailsInfo = new AccountDetailsInfoDao();
				objAccDetailsInfo = (AccountDetailsInfoDao) listAccDetails.get(j);

				System.out.println("MSISDN = " + objAccDetailsInfo.getMsisdn());
				System.out.println("ServiceStatus = " + objAccDetailsInfo.getServiceStatus());
				System.out.println("LOB = " + objAccDetailsInfo.getLob());
				System.out.println("Package = " + objAccDetailsInfo.getPackage());
				System.out.println("PrinSuppInd = " + objAccDetailsInfo.getPrinSuppInd());
				System.out.println("PostPreInd = " + objAccDetailsInfo.getPostPreInd());
				System.out.println("SSOStatus = " + objAccDetailsInfo.getSSOStatus());
			}
		}

		System.out.println("....End print output....");
	}

	private final static String getElementValue(Node elem) {
		Node node;
		if (elem != null) {
			if (elem.hasChildNodes()) {
				for (node = elem.getFirstChild(); node != null; node = node.getNextSibling()) {
					if (node.getNodeType() == Node.TEXT_NODE) {
						return node.getNodeValue();
					}
				}
			}
		}
		return "";
	}

	private static LobOutputDao setLobOutputDao (LobOutputDao dao, String propName, String prop){

		if (propName.equalsIgnoreCase("Status")){
			dao.setStatus(prop);
			//System.out.println("A setLobOutputDao " + propName + " = " + prop);
		}else if (propName.equalsIgnoreCase("Error_Code")){
			dao.setErrCode(prop);
			//System.out.println("A setLobOutputDao " + propName + " = " + prop);
		}else if (propName.equalsIgnoreCase("Error_Msg")){
			dao.setErrMsg(prop);
			//System.out.println("A setLobOutputDao " + propName + " = " + prop);
		}
		return dao;
	}

	private static LobOutputDao setLobOutputDao (LobOutputDao dao, String propName, ArrayList prop){

		if (propName.equalsIgnoreCase("Account")){
			dao.setAccount(prop);
			//System.out.println("A setLobOutputDao " + propName + " = array");
		}
		return dao;
	}

	private static AccountInfoDao setAccountInfoDao (AccountInfoDao dao, String propName, String prop){

		if (propName.equalsIgnoreCase("acctExtID")){
			dao.setAcctExtID(prop);
			//System.out.println("B setAccountInfoDao " + propName + " = " + prop);
		}
		return dao;
	}

	private static AccountInfoDao setAccountInfoDao (AccountInfoDao dao, String propName, ArrayList prop){

		if (propName.equalsIgnoreCase("acctDetails")){
			dao.setAcctDetails(prop);
			//System.out.println("B setAccountInfoDao " + propName + " = array");
		}
		return dao;
	}

	private static AccountDetailsInfoDao setAccountDetailsInfoDao (AccountDetailsInfoDao dao, String propName, String prop){

		if (propName.equalsIgnoreCase("MSISDN")){
			dao.setMsisdn(prop);
			//System.out.println("C setAccountDetailsInfoDao " + propName + " = " + prop);
		}else if (propName.equalsIgnoreCase("ServiceStatus")){
			dao.setServiceStatus(prop);
			//System.out.println("C setAccountDetailsInfoDao " + propName + " = " + prop);
		}else if (propName.equalsIgnoreCase("LOB")){
			dao.setLob(prop);
			//System.out.println("C setAccountDetailsInfoDao " + propName + " = " + prop);
		}else if (propName.equalsIgnoreCase("Package")){
			dao.setPackage(prop);
			//System.out.println("C setAccountDetailsInfoDao " + propName + " = " + prop);
		}else if (propName.equalsIgnoreCase("PrinSuppInd")){
			dao.setPrinSuppInd(prop);
			//System.out.println("C setAccountDetailsInfoDao " + propName + " = " + prop);
		}else if (propName.equalsIgnoreCase("PostPreInd")){
			dao.setPostPreInd(prop);
			//System.out.println("C setAccountDetailsInfoDao " + propName + " = " + prop);
		}else if (propName.equalsIgnoreCase("SSOStatus")){
			dao.setSSOStatus(prop);
			//System.out.println("C setAccountDetailsInfoDao " + propName + " = " + prop);
		}
		return dao;
	}

	public static LobOutputDao retrieveValue00(Node node00, int index) {
		LobOutputDao oLobOutput = new LobOutputDao();
		ArrayList arlAccount = new ArrayList();

		String node00Name = node00.getNodeName();
		String node00Value = getElementValue(node00);
		//System.out.println("00 " + node00Name + " = " + node00Value);
		NodeList children00 = node00.getChildNodes();

		//System.out.println("children00.getLength() = " + children00.getLength());
		for (int i = 1; i < children00.getLength(); i++) {
			Node node01 = children00.item(i);

			if (node01.getNodeType() == Node.ELEMENT_NODE) {
				String node01Name = node01.getNodeName();
				String node01Value = getElementValue(node01);
				//System.out.println("01 " + node01Name + " = " + node01Value);
				oLobOutput = setLobOutputDao(oLobOutput,node01Name,node01Value);
				NodeList children01 = node01.getChildNodes();

				//System.out.println("children01.getLength() = " + children01.getLength());
				for (int j = 1; j < children01.getLength(); j++) {
					AccountInfoDao oAccountInfo = new AccountInfoDao();
					boolean blnAddArrayAccInfo = false;
					ArrayList arlAcctDetails = new ArrayList();
					Node node02 = children01.item(j);

					if (node02.getNodeType() == Node.ELEMENT_NODE) {
						String node02Name = node02.getNodeName();
						String node02Value = getElementValue(node02);
						//System.out.println("02 " + node02Name + " = " + node02Value);
						NodeList children02 = node02.getChildNodes();

						//System.out.println("children02.getLength() = " + children02.getLength());
						for (int k = 1; k < children02.getLength(); k++) {
							Node node03 = children02.item(k);

							if (node03.getNodeType() == Node.ELEMENT_NODE) {
								blnAddArrayAccInfo = true;
								String node03Name = node03.getNodeName();
								String node03Value = getElementValue(node03);
								//System.out.println("03 " + node03Name + " = " + node03Value);
								oAccountInfo = setAccountInfoDao(oAccountInfo,node03Name,node03Value);
								NodeList children03 = node03.getChildNodes();

								//System.out.println("children03.getLength() = " + children03.getLength());
								for (int m = 1; m < children03.getLength(); m++) {
									AccountDetailsInfoDao oAccountDetailsInfo = new AccountDetailsInfoDao();
									boolean blnAddArrayAccDetails = false;
									Node node04 = children03.item(m);

									if (node04.getNodeType() == Node.ELEMENT_NODE) {
										String node04Name = node04.getNodeName();
										String node04Value = getElementValue(node04);
										//System.out.println("04 " + node04Name + " = " + node04Value);
										NodeList children04 = node04.getChildNodes();

										//System.out.println("children04.getLength() = " + children04.getLength());
										for (int n = 1; n < children04.getLength(); n++) {
											Node node05 = children04.item(n);

											if (node05.getNodeType() == Node.ELEMENT_NODE) {
												blnAddArrayAccDetails = true;
												String node05Name = node05.getNodeName();
												String node05Value = getElementValue(node05);
												//System.out.println("05 " + node05Name + " = " + node05Value);
												oAccountDetailsInfo = setAccountDetailsInfoDao(oAccountDetailsInfo,node05Name,node05Value);
											}
										}
									}
									if(blnAddArrayAccDetails){
										//System.out.println("#2 arlAcctDetails.add() = oAccountDetailsInfo");
										arlAcctDetails.add(oAccountDetailsInfo);
									}

								}
								if (arlAcctDetails.size()>0){
									//System.out.println("##2 arlAcctDetails.size() = " + arlAcctDetails.size());
									oAccountInfo = setAccountInfoDao(oAccountInfo,"acctDetails",arlAcctDetails);
								}
							}
						}
					}
					if (blnAddArrayAccInfo){
						//System.out.println("#1 arlAccount.add() = oAccountInfo");
						arlAccount.add(oAccountInfo);
					}

				}
				if (arlAccount.size()>0){
					//System.out.println("##1 arlAccount.size() = " + arlAccount.size());
					oLobOutput = setLobOutputDao(oLobOutput,"Account",arlAccount);
				}
			}
		}
		return oLobOutput;
	}



}